﻿// ===============================================================================
// Project Name        :    Gfw.BLL.Basic.Facilities
// Project Description :    
// ===============================================================================
// Class Name          :    ManagementAward
// Namespace:          :    Gfw.BLL.Basic.Facilities
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/6/24 21:20:27
// Update Time         :    2012/6/24 21:20:27
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================

using Gfw.Infrastructure.Account;
using Gfw.Models.Admin;
using Gfw.Repositories.Admin;
using Gfw.Repository;

namespace Gfw.Infrastructure.Facilities
{

    public class ManagementAward : IFacility
    {
        private const decimal ParentRatio = 0.15M;
        private const decimal GrandRatio = 0.07M;
        private string currentUserId;
        private decimal _amount;

        private ManagementAward(string currentUserId,decimal amount)
        {
            _amount = amount;
            this.currentUserId = currentUserId;
        }

        public void Execute(Repository.UnitOfWork unitOfWork = null)
        {
            UnitOfWork work = unitOfWork ?? new UnitOfWork();

            //获取基本的用户信息
            UsrUserRepository userRepository = new UsrUserRepository();
            var currentUser = userRepository.Get(currentUserId, work);

            //判断父节点是否具有管理奖，如果父节点没有管理奖，则祖父节点也没有管理奖
            //判断当前用户是否具有兄弟节点
           if( userRepository.HasBrother(currentUser,true,work))
           {
               UsrUser parent = userRepository.GetParent(currentUser, work);

               //开始计算父节点的管理奖
               decimal parentAward = _amount*ParentRatio;

               CompanyDynamicAward parentDa = new CompanyDynamicAward(parent.Id, parentAward,
                                                                      UserTradeTypeEnum.Management,
                                                                      CompanyTradeTypeEnum.Management);
               parentDa.Execute(work);

               //判断祖父节点的管理奖
               if (userRepository.HasFullCousin(currentUser,true, work))
               {
                   UsrUser grand = userRepository.GetParent(parent, work);

                   decimal grandAward = _amount*GrandRatio;
                   CompanyDynamicAward grandDa = new CompanyDynamicAward(grand.Id, grandAward,
                                                                         UserTradeTypeEnum.Management,
                                                                         CompanyTradeTypeEnum.Management);
                   parentDa.Execute(work);
               }

           }

            //如果当前规则没有挂接事务，则提交事务
           if (unitOfWork == null)
               work.Commit();
        }
    }
}
